package com.itheima.web.controller.sms;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.service.sms.IFlashPromotionService;
import com.itheima.utils.PageResultList;
import com.itheima.utils.ResultData;
import com.itheima.vo.sms.FlashPromotion;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

/**
 * 限时购表 前端控制器
 */
@RestController
@RequestMapping("/flash")
public class FlashPromotionController {
    @DubboReference
    private IFlashPromotionService flashPromotionService;

    @PostMapping("/create")
    public ResultData save(@RequestBody FlashPromotion flashPromotion){
        flashPromotionService.save(flashPromotion);
        return ResultData.ok("","");
    }
    @PostMapping("/delete/{id}")
    public ResultData delete(@PathVariable Long id){
        flashPromotionService.removeById(id);
        return ResultData.ok("","");
    }
    @GetMapping("/list")
    public ResultData list(String keyword, Integer pageNum, Integer pageSize){
        Page<FlashPromotion> list=flashPromotionService.getPage(keyword,pageSize,pageNum);
        PageResultList pageResult=new PageResultList();
        pageResult.setList(list.getRecords());
        pageResult.setPageSize(list.getSize());
        pageResult.setTotalPage(list.maxLimit());
        pageResult.setTotal(list.getTotal());
        pageResult.setPageNum(list.getPages());

       return ResultData.ok("",pageResult);
    }
    @PostMapping("/update/status/{id}")
    public ResultData updateStatus(@PathVariable("id") Long id,@RequestParam Integer status){
        FlashPromotion flashPromotion=new FlashPromotion();
       flashPromotion.setId(id);
       flashPromotion.setStatus(status);
       flashPromotionService.updateById(flashPromotion);
       return ResultData.ok("修改成功","");
    }

    @PostMapping("/update/{id}")
    public ResultData update(@PathVariable("id") Long id,@RequestBody  FlashPromotion flashPromotion){
        flashPromotion.setId(id);
        flashPromotionService.updateById(flashPromotion);
        return ResultData.ok("修改成功","");
    }

    @GetMapping("/{id}")
    public ResultData getFlashPromotion(@PathVariable Long id){
        FlashPromotion byId = flashPromotionService.getById(id);
        return ResultData.ok("",byId);
    }
}

